﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace 选择框组合文本框
{
    /// <summary>
    /// ComboBoxText.xaml 的交互逻辑
    /// </summary>
    public partial class ComboBoxText : UserControl
    {
        #region 控件事件
        public event RoutedEventHandler TextBoxChanged;
        public event RoutedEventHandler ButtonClick;
        public event RoutedEventHandler GetComboBoxIndex;
        #endregion

        #region 公共方法

        /// <summary>
        /// Add ComboBox Data.
        /// <para>添加ComboBox数据项</para>
        /// </summary>
        /// <param name="content"></param>
        /// <param name="color"></param>
        public void Add(string content, string color = "#000000")
        {
            // 创建新的 ComboBoxItem
            var item = new ComboBoxItem
            {
                Content = content,
                Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(color ?? color))
            };
            // 将新项添加到 ComboBox 中
            Select.Items.Add(item);
        }

        /// <summary>
        /// Is Input TextBox Null.
        /// <para>判断输入框是否为空</para>
        /// </summary>
        /// <returns></returns>
        public bool IsInputNull()
        {
            this.InputTextNull = this.Input.Text == null || string.IsNullOrEmpty(this.Input.Text);
            return this.InputTextNull;
        }

        #endregion

        #region 依赖属性

        #region 是否为空
        public static readonly DependencyProperty InputTextNullProperty =
        DependencyProperty.Register("InputTextNull", typeof(bool), typeof(ComboBoxText), new PropertyMetadata(false));

        /// <summary>
        /// IsInputTextNull
        /// <para>输入的文本是否为空</para>
        /// </summary>
        public bool InputTextNull
        {
            get { return (bool)GetValue(InputTextNullProperty); }
            set { SetValue(InputTextNullProperty, value); }
        }
        #endregion

        #region 输入文本
        // Define the ButtonContent dependency property
        public static readonly DependencyProperty InputTextProperty =
        DependencyProperty.Register("InputText", typeof(string), typeof(ComboBoxText), new PropertyMetadata(string.Empty));

        /// <summary>
        /// ButtonContent property
        /// <para>输入文本</para>
        /// </summary>
        public string InputText
        {
            get { return (string)GetValue(InputTextProperty); }
            set { SetValue(InputTextProperty, value); }
        }
        #endregion

        #region 按钮文本
        // Define the ButtonContent dependency property
        public static readonly DependencyProperty ButtonContentProperty =
        DependencyProperty.Register("ButtonContent", typeof(string), typeof(ComboBoxText), new PropertyMetadata(string.Empty));

        /// <summary>
        /// ButtonContent property
        /// <para>设置按钮文本</para>
        /// </summary>
        public string ButtonContent
        {
            get { return (string)GetValue(ButtonContentProperty); }
            set { SetValue(ButtonContentProperty, value); }
        }
        #endregion

        #region 文本水印
        // Define the Watermark dependency property
        public static readonly DependencyProperty WatermarkProperty =
        DependencyProperty.Register("Watermark", typeof(string), typeof(ComboBoxText), new PropertyMetadata(string.Empty));

        /// <summary>
        /// Watermark property
        /// <para>设置文本水印</para>
        /// </summary>
        public string Watermark
        {
            get { return (string)GetValue(WatermarkProperty); }
            set { SetValue(WatermarkProperty, value); }
        }
        #endregion

        #region 获取索引
        // Define the Watermark dependency property
        public static readonly DependencyProperty GetSelectedIndexProperty = DependencyProperty.Register("GetSelectedIndex", typeof(int), typeof(ComboBoxText), new PropertyMetadata(0));

        /// <summary>
        /// Watermark property
        /// <para>获取ComboBox索引</para>
        /// </summary>
        public int GetSelectedIndex
        {
            get { return (int)GetValue(GetSelectedIndexProperty); }
            set { SetValue(GetSelectedIndexProperty, value); }
        }
        #endregion

        #region 按钮等待
        public static readonly DependencyProperty ButtonCancelContentProperty = DependencyProperty.Register("ButtonCancelContent", typeof(string), typeof(ComboBoxText), new PropertyMetadata(string.Empty));

        /// <summary>
        /// <para>按钮等待文本</para>
        /// </summary>
        public string ButtonCancelContent
        {
            get { return (string)GetValue(ButtonCancelContentProperty); }
            set { SetValue(ButtonCancelContentProperty, value); }
        }
        #endregion

        #endregion

        public ComboBoxText()
        {
            InitializeComponent();

            this.Input.TextChanged += (s, e) => InputChanged();
            this.Send.Click += (s, e) => Send_Click();
            this.Select.SelectionChanged += Select_SelectionChanged;
        }

        /// <summary>
        /// ComboBox Changed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Select_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GetComboBoxIndex?.Invoke(this, e);
        }

        /// <summary>
        /// Input Text Changed.
        /// </summary>
        private void InputChanged()
        {
            this.InputTextNull = false;
            TextBoxChanged?.Invoke(this, new RoutedEventArgs());
        }

        /// <summary>
        /// Button Click.
        /// </summary>
        private void Send_Click()
        {
            if (IsInputNull())
            {
                this.Send.IsChecked = false;
                return;
            }
            ButtonClick?.Invoke(this, new RoutedEventArgs());
        }

    }
}
